/**
 * 
 */
package com.logistics.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.logistics.Orders;
import com.logistics.OrdersQueryParam;
import com.logistics.Template;
import com.logistics.admin.AdminGenericBaseAction;
import com.logistics.admin.DateUtils;
import com.logistics.admin.WebSendSmsClient;
import com.logistics.service.OrdersService;
import com.logistics.service.TemplateService;

/**
 * @author hzz
 *
 * 2014年3月29日
 */

@Component("ordersAdminAction")
@Scope("prototype")
public class OrdersAdminAction extends AdminGenericBaseAction<Orders, OrdersService> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private OrdersService service;

	@Autowired
	public void setService(OrdersService service) {
		this.service = service;
	}
	
	private TemplateService templateService;

	@Autowired
	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}

	@Override
	public OrdersService getService() {
		return service;
	}
	
	private Orders orders;

	public Orders getOrders() {
		return orders;
	}

	public void setOrders(Orders orders) {
		this.orders = orders;
	}

	@Override
	protected Orders initEntityForAdd() {
		return orders;
	}
	
	private List<Orders> list;

	public List<Orders> getList() {
		return list;
	}

	public void setList(List<Orders> list) {
		this.list = list;
	}
	
	private OrdersQueryParam param;
	
	public OrdersQueryParam getParam() {
		return param;
	}

	public void setParam(OrdersQueryParam param) {
		this.param = param;
	}
	
	private List<Template> templates;

	public List<Template> getTemplates() {
		return templates;
	}

	public void setTemplates(List<Template> templates) {
		this.templates = templates;
	}

	public String list() {
		if (param == null) {
			param = new OrdersQueryParam();
			Date now = new Date();
			param.setStartDate(DateUtils.formatDate(now));
			param.setEndDate(DateUtils.formatDate(now));
		}
		
		templates = templateService.query(null);
		param.setPaging(getPaging());
		list = service.query(param);
		
		return LIST;
	}
	
	public void saveForJson() {
		if (StringUtils.isNotBlank(getId()) || StringUtils.isBlank(orders.getPhone())) {
			outputJsonWithException("phoneIsNull");
		}
		
		orders.setEntityId(getCurrentOrderNumber());
		orders.setCreateDate(new Date());
		orders.setStatus(true);
		orders.setOrderStatus(true);
		try {
			service.save(orders);
			
			WebSendSmsClient sendSmsClient = new WebSendSmsClient();
			String sign = ("【" + orders.getSign() + "】");
			String sendContent = orders.getContent() + "取件码：" + orders.getEntityId() + sign;
			String res = sendSmsClient.sendSms(orders.getPhone(), sendContent);
			System.out.println(res);
		} catch (Exception e) {
			outputJsonWithException("PrimaryKeyConflict");
			return;
		}
			
		outputSimpleSuccessJson();
		
	}
	
	private String getCurrentOrderNumber() {
		
		Orders order = service.todayFinallyOrders();
		Date now = new Date();
		SimpleDateFormat format = new SimpleDateFormat("dd");
		String prefix = format.format(now);
		if (order == null || StringUtils.isBlank(order.getEntityId())) {
			return prefix + "001";
		}
		String orderNo = order.getEntityId().substring(2);
		int number = Integer.valueOf(orderNo)+1;
		
		
		switch (String.valueOf(number).length()) {
			case 1:
				return prefix + "00" + number;
			case 2:
				return prefix + "0" + number;
			case 3:
				return prefix + String.valueOf(number);
			default :
				return prefix + "001";
		}
		
	}

}
